package com.cg.controller;


import com.cg.common.Result;
import com.cg.pojo.entity.UserEntity;
import com.cg.pojo.vo.UserRegisterVo;
import com.cg.service.UserService;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import jakarta.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping(("/user"))
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;


    @RequestMapping("/list")
    @ResponseBody
    public Result<List<UserRegisterVo>> getList() {

        List<UserRegisterVo> userList = userService.getList();
        return Result.succsee(userList);
    }

    @RequestMapping("/login")
    @ResponseBody
    public Result login(@RequestBody Map<String, String> map, HttpSession session, HttpServletResponse response) {
        String username = map.get("username");
        String password = map.get("password");

        // 校验用户名和密码是否正确
        boolean flag = userService.login(username, password);

        if (flag) {
            // 登录成功，获取用户信息
            UserEntity user = userService.getUserByUsername(username);
            System.out.println("id: " + user.getId() + " username: " + user.getUsername() + " name: " + user.getName() +
                    " password: " + user.getPassword() + " age: " + user.getAge() + " phone: " + user.getPhone() + " email: " + user.getEmail());

            // 将用户名存入 session
            session.setAttribute("username", user.getUsername());

            // 设置 Cookie，保存用户名，过期时间为 1 小时
            Cookie usernameCookie = new Cookie("username", user.getUsername());
            usernameCookie.setMaxAge(3600);  // 1小时
            usernameCookie.setPath("/");  // 设置路径为根路径，这样整个网站都可以访问此 Cookie
            response.addCookie(usernameCookie);
            System.out.println("登录成功");
            // 返回成功结果
            return Result.succsee(user);
        } else {
            // 登录失败，返回错误信息
            return Result.error("用户名或密码错误！");
        }
    }



    @RequestMapping("/getUserInfo")
    @ResponseBody
    public Result getUserInfo(HttpSession session) {

        String username = (String) session.getAttribute("username");
        System.out.println(username);
        if (username != null) {
            return Result.succsee(username);  // 返回用户信息
        } else {
            return Result.error("未登录");
        }
    }


    @RequestMapping("/register")
    @ResponseBody
    public Result<String> register(@RequestBody UserRegisterVo user) {
        boolean flag = userService.register(user);
        if (flag) {
            return Result.succsee("注册成功！");
        } else {
            return Result.error("注册失败！");
        }
    }

    @RequestMapping("/logout")
    @ResponseBody
    public Result<String> logout() {
        return Result.succsee("退出成功！");
    }


    @RequestMapping("/getUser")
    @ResponseBody
    public Result<UserEntity> getUserByUsername(@RequestParam("username") String username) {
        UserEntity user = userService.getUserByUsername(username);
        return Result.succsee(user);
    }

    @RequestMapping("/updateUser")
    @ResponseBody
    public Result<String> updateUser(@RequestBody UserRegisterVo user){
        userService.updateUser(user);
        return Result.succsee("更新成功！");
    }
}
